<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button v-if="$AUTH('system:sysRegion:add')" type="primary" icon="el-icon-plus" @click="add"></el-button>
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <selectRegion ref="selectRegionRef" :parentLoadingFunction="parentLoadingFunction" :parentGetData="parentGetData" :selectStyle="'width:15rem;border: 1px solid red;'"/>
          <el-input v-model="search.name" placeholder="名称" clearable></el-input>
		  <!-- <el-select  v-model="search.type" placeholder="类型" clearable>
			  <el-option :value="1">省份</el-option><el-option :value="2">城市</el-option>
			  <el-option :value="3">区县</el-option><el-option :value="4">街道</el-option>
		  </el-select>
          <el-input v-model="search.telCode" placeholder="区号" clearable></el-input> -->
          <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding" v-loading="showloading">
      <scTable ref="table" :apiObj="apiObj" row-key="sysRegionId" :loadNow="false" :pageSize="100" @selection-change="selectionChange" stripe>
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column label="名称" prop="name" width="260"></el-table-column>
        <el-table-column label="简称" prop="shortName" width="220"></el-table-column>
		<el-table-column label="类型" width="100">
			<template #default="scope">
				<span v-if="scope.row.type==1">省份</span><span v-if="scope.row.type==2">城市</span>
				<span v-if="scope.row.type==3">区县</span><span v-if="scope.row.type==4">街道</span>
			</template>
		</el-table-column>
        <el-table-column label="路径" prop="regionPath" width="230"></el-table-column>
        <el-table-column label="区号" prop="telCode" width="100"></el-table-column>
        <el-table-column label="经;纬度" prop="latitude" width="170">
			<template #default="scope"><span>{{scope.row.longitude}}</span>&nbsp;;&nbsp;<span>{{scope.row.latitude}}</span></template>
		</el-table-column>
        <el-table-column label="排序" prop="sort" width="70"></el-table-column>
        <el-table-column label="操作" fixed="right" align="right">
          <template #default="scope">
            <el-button v-if="$AUTH('system:sysRegion:edit')" type="text" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button v-if="$AUTH('system:sysRegion:del')" type="text" size="small" @click="table_del(scope.row, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>

  <sysRegionEdit-dialog v-if="dialog.sysRegionEdit" ref="sysRegionEditDialog" @success="handleSaveSuccess" @closed="dialog.sysRegionEdit=false"></sysRegionEdit-dialog>
</template>

<script>
  import systemAPI from '@/api/system/sysRegion';
  import sysRegionEditDialog from './sysRegionEdit';//1--导入
  import selectRegion from '@/components/glSelectCO/selectRegion';
  export default {
    name: 'sysRegion',
    components: {
        sysRegionEditDialog,selectRegion,
    },
    data() {
      return {
        dialog: {
          sysRegionEdit: false
        },
        apiObj: systemAPI.sysRegion.listSelect,
        selection: [],
        search: {
          keyword: null,
          createTime: null
        },
		showloading: false,
      }
    },
	mounted() {
		//let datas = this.$refs.table.tableData;
		//this.$refs.selectRegionRef.toInitData(datas);
	},
    methods: {
      //添加
      add(){
        this.dialog.sysRegionEdit = true
        this.$nextTick(() => {
          this.$refs.sysRegionEditDialog.open()
        })
      },
      //编辑
      table_edit(row){
        this.dialog.sysRegionEdit = true
        this.$nextTick(() => {
          this.$refs.sysRegionEditDialog.open('edit').setData(row)
        })
      },
      //删除
      async table_del(row){
        var confirm = await this.$confirm('确定删除选中的项吗？', '提示', {
          confirmButtonText: 'ok',
          type: 'warning'
        }).catch(() => {})
        if(confirm !== 'confirm'){
          return false;
        }
        var res = await systemAPI.sysRegion.delete.delete(row.sysRegionId);
        if(res.code === 2000){
          this.$refs.table.refresh();
          this.$message.success("删除成功");
        }else{
          this.$alert(res.message, "提示", {type: 'error'});
        }
      },
      //表格选择后回调事件
      selectionChange(selection){
        this.selection = selection;
      },
      //搜索
      upsearch(){
        this.$refs.table.upData(this.search);
      },
      //本地更新数据
      handleSaveSuccess(){
        this.$refs.table.refresh();
      },
      handlePermissionSuccess(){
        this.$refs.table.refresh();
      },
      handleDataSuccess(){
        this.$refs.table.refresh();
      },
      formatDate(row,column) {// 获取单元格数据
        let data = row[column.property];
        if(data == null) {
          return null;
        }
        let dt = new Date(data);
        return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds();
      },
	  parentLoadingFunction(loadingBz){//选择系统---- 加载提醒
	  	this.showloading = loadingBz;
	  },
	  parentGetData(resData){//选择 系统回调函数
		if(resData.code === 2000){
			this.$refs.table.customOnePageData(resData.data.records);
		}else{
			this.$message.warning(resData.message);
			this.$refs.table.customOnePageData();
		}
	  },
    }
  }
</script>
<style>
</style>
